package com.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.common.Result;
import com.wms.entity.User;
import com.wms.mapper.UserMapper;
import com.wms.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wms
 * @since 2022-07-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
    @Override
    public Result list(User user, Integer pageNum, Integer pageSize) {
        Page page=new Page<>(pageNum,pageSize);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if(user.getName()!=null&&!user.getName().equals("")){
            queryWrapper.like(StringUtils.isNotBlank(user.getName()), "NAME", user.getName());
        }
        if(user.getSex()!=null&&!user.getSex().equals("")){
            queryWrapper.eq("sex",user.getSex());
        }

        IPage<User> list = userService.pageMaps(page, queryWrapper);
        Long aLong = userMapper.selectCount(null);
        HashMap hashMap=new HashMap();
        hashMap.put("userlist",list.getRecords());
        return Result.suc(hashMap,aLong);
    }

    @Override
    public Result add(User user) {
//    QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        int insert = userMapper.insert(user);
        return Result.suc(insert);
    }

    @Override
    public Result delete(int id) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int delete = userMapper.delete(queryWrapper);
        return Result.suc(delete);
    }

    @Override
    public Result select(User user) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",user.getId());
        User user1 = userMapper.selectOne(queryWrapper);
        return Result.suc(user1);
    }

    @Override
    public Result update(User user) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",user.getId());
        int update = userMapper.update(user, queryWrapper);
        return Result.suc(update);
    }

    @Override
    public Result findByNo(String no) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("no",no);
        User user = userMapper.selectOne(queryWrapper);
        if (user!=null){
            return Result.suc(user);
        }else {
            return Result.fail();
        }

    }

    @Override
    public IPage pageCC(IPage<User> page, Wrapper wrapper) {
        return userMapper.pageCC(page,wrapper);
    }
}
